import { defineStore } from "pinia";
import { reactive, ref } from "vue";
import { apiLogin, apiLogout, apiRegister, apiUserInfo ,apiChangeUsername, apiChangePassword} from "../api/modules/api.user_login";
import { SetToken, GetToken, RemoveToken } from "@/utils/storage.js";

export const useUserStore = defineStore("user", () => {
  const token = ref(GetToken() || "");
  const loginStatus = ref(token.value !== "");
  const userInfo=reactive({
    username:'',
    avatar:'',
  })
  const SetTokenPinia = (newToken) => {
    // 注意避免命名冲突
    token.value = newToken;
    loginStatus.value = true;
    SetToken(newToken);
  };
  const RemoveTokenPinia = () => {
    token.value = "";
    loginStatus.value = false;
    RemoveToken();
  };
// 登录
  const LoginPinia = async (LoginForm) => {
    try {
      const res = await apiLogin(LoginForm);
      if (res && res.token) {
        SetTokenPinia(res.token);
        return { success: true, data: res };
      } else {
        return { success: false, data: res.message || '登录失败' };
      }
    } catch (error) {
      return { success: false, data: error.message || '登录失败' };
    }
  }
  // 注册
  const RegisterPinia = async (RegisterForm) => {
    try {
      const res = await apiRegister(RegisterForm);
      if (res) {
        return { success: true, data: res };
      } else {
        return { success: false, data: '注册失败' };
      }
    } catch (error) {
      return { success: false, data: error.message || '注册失败' };
    }
  };

  const LogoutPinia = async () => {
    try { 
      const res = await apiLogout();
      // 根据后端返回格式判断成功状态
      if (res.status === 200) {
        RemoveTokenPinia();
        return { success: true, data: res };
      } else {
        return { success: false, data: res.data?.message || '退出登录失败' };
      }
    } catch (error) {
      return { success: false, data: error.message || '退出登录失败' };
    }
  };

  // 获取用户信息
  const getUserInfoPinia = async () => {
    try {
      const res = await apiUserInfo();
      if (res.status === 200) {
        userInfo.username = res.data.userInfo.username;
      userInfo.avatar = import.meta.env.VITE_BACKEND_URL+
        res.data.userInfo.user_pic;
      }
      return { success: true, data: res };
    } catch (error) {
      return { success: false, data: error.message || '获取用户信息失败' };
    }
  }
  // 更换昵称
  const changeUserName = async (data) => { 
    try { 
      const res = await apiChangeUsername(data)
      if (res.status === 200) {
        await getUserInfoPinia();
        return { success: true, data: res };
      } else {
        return { success: false, data: res.data?.message || '更换昵称失败' };
      }
    } catch (error) {
      return { success: false, data: error.message || '更换昵称失败' };
    }
  }
  
  // 更新头像
  const updateAvatar = (newAvatarUrl) => {
    userInfo.avatar = newAvatarUrl;
  }

  // 修改密码
  const changePassword = async (data) => {
    try {
      const res = await apiChangePassword(data);
      if (res.status === 200) {
        return { success: true, data: res };
      } else {
        return { success: false, data: res.data?.message || '修改密码失败' };
      }
    } catch (error) {
      return { success: false, data: error.message || '修改密码失败' };
    }
  }
  
  return { loginStatus, token, SetTokenPinia, RemoveTokenPinia, LoginPinia, RegisterPinia, LogoutPinia,userInfo ,getUserInfoPinia,changeUserName,updateAvatar,changePassword};
});
